import org.junit.jupiter.api.Test;

import java.security.MessageDigest;

/**
 * @author: hfxiang
 * @Description:
 * @date: 2022/11/9 14:25
 */
public class KujiaLTester {


    @Test
    public void printWithoutAppUid() {
        System.out.println(getUrlParamStr());
    }
    @Test
    public void printEmail1() {
        String appuid = "068abfe9f0274ba6ab78c54ce9c87fd1";//ceshi1@hxr.com
        System.out.println(getUrlParamStr(appuid));
    }
    @Test
    public void printEmail2() {
        String appuid = "ebd382fa2a544038b468a088292f5a61";//ceshi2@hxr.com
        System.out.println(getUrlParamStr());
    }

    //获取sign并拼接为查询参数
    private String getUrlParamStr() {

        long timestamp = System.currentTimeMillis();
        String sign  = md5(appSecretT + appKeyT + timestamp);
        return "?appkey="+appKeyT+"&timestamp="+timestamp+"&sign="+sign;
    }

    //获取sign并拼接为查询参数
    private String getUrlParamStr(String appuid) {
        long timestamp = System.currentTimeMillis();
        String sign  = md5(appSecretT + appKeyT  +appuid+ timestamp);
        return "?appkey="+appKeyT+"&timestamp="+timestamp+"&sign="+sign+"&appuid="+appuid;
    }

    public static String md5(String str) {

        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (java.security.NoSuchAlgorithmException ex) {
            //断言失败：JDK 带有 MD5 算法实现
            throw new AssertionError(ex);
        }
        byte[] res = md.digest(str.getBytes());
        return dumpBytes(res);
    }
    private static String dumpBytes(byte[] bytes) {
        int size = bytes.length;
        StringBuffer sb = new StringBuffer(size * 2);
        String s;
        for (int i = 0; i < size; ++i) {
            s = Integer.toHexString(bytes[i]);
            if (s.length() == 8) {          // -128 <= bytes[i] < 0
                sb.append(s.substring(6));
            } else if (s.length() == 2) {   // 16 <= bytes[i] < 128
                sb.append(s);
            } else {
                sb.append("0" + s);         // 0 <= bytes[i] < 16
            }
        }
        return sb.toString();
    }
}
